<?php

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');

/**
 * Component Items Model
 */
class TaxiBookingModelOrders extends JModelList {

    /**
     * Items data
     *
     * @var object
     */
    var $_data = null;

    /**
     * Items total
     *
     * @var integer
     */
    var $_total = null;

    /**
     * Pagination object
     *
     * @var object
     */
    var $_pagination = null;

    /**
     * Item id
     *
     * @var int
     */
    var $_id = null;

    /**
     * Constructor
     *
     * @since 1.0
     */
    function __construct() {
        parent::__construct();

//		global $mainframe, $option;
        $mainframe = JFactory::getApplication();
        $limit = $this->getUserStateFromRequest($this->context . '.limit', 'limit', $jconfig->list_limit, 'int');
        $limitstart = $this->getUserStateFromRequest($this->context . '.limitstart', 'limitstart', $jconfig->list_limit, 'int');


        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);

        $array = JRequest::getVar('cid', 0, '', 'array');
        $this->setId((int) $array[0]);
    }

    /**
     * Method to set the Items identifier
     *
     * @access	public
     * @param	int Category identifier
     */
    function setId($id) {
        // Set id and wipe data
        $this->_id = $id;
        $this->_data = null;
    }

    /**
     * Method to get item data
     *
     * @access public
     * @return object
     */
    function getData() {
        // Lets load the Items if it doesn't already exist
        if (empty($this->_data)) {
            $query = $this->_buildQuery();
            $this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
        }

        return $this->_data;
    }

    /**
     * Method to get the total nr of the Items
     *
     * @access public
     * @return integer
     */
    function getTotal() {
        // Lets load the Items if it doesn't already exist
        if (empty($this->_total)) {
            $query = $this->_buildQuery();
            $this->_total = $this->_getListCount($query);
        }

        return $this->_total;
    }

    /**
     * Method to get a pagination object for the Items
     *
     * @access public
     * @return object
     */
    function getPagination() {
        // Lets load the Items if it doesn't already exist
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit'));
        }

        return $this->_pagination;
    }

    /**
     * Method to build the query for the Items
     *
     * @access private
     * @return string
     * @since 1.0
     */
    function _buildQuery() {
        // Get the WHERE and ORDER BY clauses for the query
        $where = $this->_buildContentWhere();
        $orderby = $this->_buildContentOrderBy();

        $query = 'SELECT *'
                . ' FROM #__taxibooking_orders'
                . $where
                . $orderby
        ;

        return $query;
    }

    /**
     * Method to build the orderby clause of the query for the Items
     *
     * @access private
     * @return string
     * @since 1.0
     */
    function _buildContentOrderBy() {
        $filter_order = $this->getUserStateFromRequest($this->context . '.orders.filter_order', 'filter_order', 'datetime1', 'cmd'); //$mainframe->getUserStateFromRequest( $option.'.points.filter_order', 		'filter_order', 	'p.ordering', 'cmd' );
        $filter_order_Dir = $this->getUserStateFromRequest($this->context . '.orders.filter_order_Dir', 'filter_order_Dir', ' DESC', 'word'); // $mainframe->getUserStateFromRequest( $option.'.points.filter_order_Dir',	'filter_order_Dir',	'', 'word' );

        if (trim($filter_order) == '')
            $filter_order = ' datetime1 ';
        if (trim($filter_order_Dir) == '')
            $filter_order_Dir = ' DESC ';

        $orderby = ' ORDER BY ' . $filter_order . ' ' . $filter_order_Dir . ', created DESC ';

        return $orderby;
    }

    /**
     * Method to build the where clause of the query for the Items
     *
     * @access private
     * @return string
     * @since 1.0
     */
    function _buildContentWhere() {
//		global $mainframe, $option;
        $mainframe = JFactory::getApplication();
        $dt1 = booking_helper::DateTime2(JRequest::getVar('fromdate', ''), JRequest::getVar('fromtime', ''));
        $dt2 = booking_helper::DateTime2(JRequest::getVar('todate', ''), JRequest::getVar('totime', ''));

//        $filter_state = $mainframe->getUserStateFromRequest($option . '.orders.filter_state', 'filter_state', '', 'word');
//        $filter_payment = $mainframe->getUserStateFromRequest($option . '.orders.filter_payment', 'filter_payment', '', 'word');
//        $search = $mainframe->getUserStateFromRequest($option . '.orders.search', 'search', '', 'string');
//        $search = $this->_db->getEscaped(trim(JString::strtolower($search)));
        $filter_state = $this->getUserStateFromRequest($this->context . '.orders.filter_state', 'filter_state', '', 'word'); //$mainframe->getUserStateFromRequest( $option.'.points.filter_state', 'filter_state', '', 'word' );
        $filter_payment = $this->getUserStateFromRequest($this->context . '.orders.filter_payment', 'filter_payment', '', 'word'); //$mainframe->getUserStateFromRequest( $option.'.points.filter_state', 'filter_state', '', 'word' );
        $search = $this->getUserStateFromRequest($this->context . '.orders.search', 'search', '', 'string');
        $search = $this->_db->getEscaped(trim(JString::strtolower($search)));
        
        $where = array();

        if ($filter_state) {
            if ($filter_state == 'P') {
                $where[] = 'state = 1';
            } else if ($filter_state == 'U') {
                $where[] = 'state = 0';
            } else if ($filter_state == 'A') {
                $where[] = 'state = -1';
            } else if ($filter_state == 'W') {
                $where[] = 'state = -2';
            }
        } else {
            //$where[] = ' state != -1';
        }

        if ($filter_payment) {
            if ($filter_payment == 'cash') {
                $where[] = 'payment = "cash"';
            } else if ($filter_payment == 'paypal_confirmed') {
                $where[] = 'payment = "paypal" AND EXISTS (SELECT item_number FROM #__taxibooking_paypal_table WHERE confirm=item_number)';
            } else if ($filter_payment == 'paypal_notconf') {
                $where[] = 'payment = "paypal" AND NOT EXISTS (SELECT item_number FROM #__taxibooking_paypal_table WHERE confirm=item_number)';
            }
        }

        if ($dt1 && $dt2) {
            $where[] = ' (datetime1 BETWEEN "' . $dt1 . '" AND "' . $dt2 . '") OR (datetime2 BETWEEN "' . $dt1 . '" AND "' . $dt2 . '")';
        }

        if ($search) {
            $where[] = ' LOWER(title) LIKE ' . $this->_db->Quote('%' . $this->_db->getEscaped($search, true) . '%', false);
        }

        $where = ( count($where) ? ' WHERE ' . implode(' AND ', $where) : '' );

        return $where;
    }

}

?>